﻿#region Copyright (c) 2000-2013 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2013 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2013 Developer Express Inc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UColor.Core.Helpers;
using System.Windows;
using UColor.WPF.Internal;
using UColor.WPF.Helpers;
using DevExpress.Xpf.Core;
using System.Collections.ObjectModel;
#if SL
using DevExpress.Xpf.Core.WPFCompatibility.Helpers;
#else
using DevExpress.Internal.DXWindow;
#endif
namespace UColor.WPF {
	public class LinkClickEventArgs : EventArgs {
		public LinkClickEventArgs(string uriString, string target) {
			UriString = uriString;
			Target = target;
		}
		public string UriString { get; private set; }
		public string Target { get; private set; }
	}
	public class ModuleAppearEventArgs : EventArgs {
		public ModuleAppearEventArgs(FrameworkElement demoModule, Exception demoModuleException) {
			DemoModule = demoModule;
			DemoModuleException = demoModuleException;
		}
		public FrameworkElement DemoModule { get; private set; }
		public Exception DemoModuleException { get; private set; }
	}
	public sealed class BaseControl : PartsControl {
		#region Dependency Properties
		public static readonly DependencyProperty DataProperty;
		public static readonly DependencyProperty StateProperty;
		public static readonly DependencyProperty ActualPageProperty;
		public static readonly DependencyProperty AllowRunAnotherDemoProperty;
		static BaseControl() {
			Type ownerType = typeof(BaseControl);
			//DataProperty = DependencyProperty.Register("Data", typeof(DemoBaseControlData), ownerType, new PropertyMetadata(null, RaiseDataChanged));
			StateProperty = DependencyProperty.Register("State", typeof(BaseControlState), ownerType, new PropertyMetadata(null, RaiseStateChanged));
			ActualPageProperty = DependencyProperty.Register("ActualPage", typeof(BaseControlPage), ownerType, new PropertyMetadata(BaseControlPage.None, RaiseActualPageChanged));
			AllowRunAnotherDemoProperty = DependencyProperty.Register("AllowRunAnotherDemo", typeof(bool), ownerType, new PropertyMetadata(false, RaiseAllowRunAnotherDemoChanged));
		}
		//DemoBaseControlData dataValue = null;
		BaseControlState stateValue = null;
		BaseControlPage actualPageValue = BaseControlPage.None;
		static void RaiseDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
		//	((BaseControl)d).dataValue = (DemoBaseControlData)e.NewValue;
		}
		static void RaiseStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			((BaseControl)d).stateValue = (BaseControlState)e.NewValue;
			((BaseControl)d).RaiseStateChanged(e);
		}
		static void RaiseActualPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			((BaseControl)d).actualPageValue = (BaseControlPage)e.NewValue;
			((BaseControl)d).RaiseActualPageChanged(e);
		}
		static void RaiseAllowRunAnotherDemoChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			((BaseControl)d).RaiseAllowRunAnotherDemoChanged(e);
		}
		#endregion
		
		BaseControlMainPart mainPart;
        public BaseControlMainPart ControlMainPart
        {
            get
            {
                return mainPart;
            }
        }
		public BaseControl() {
			
			FocusHelper.SetFocusable(this, false);
		}
		
		public BaseControlState State { get { return stateValue; } set { SetValue(StateProperty, value); } }
		public BaseControlPage ActualPage { get { return actualPageValue; } private set { SetValue(ActualPageProperty, value); } }
		
		public bool AllowRunAnotherDemo { get { return (bool)GetValue(AllowRunAnotherDemoProperty); } set { SetValue(AllowRunAnotherDemoProperty, value); } }
		public Exception DemoModuleException { get; private set; }
		public FrameworkElement CurrentDemoModule { get; private set; }
		public DemoModuleControlView DemoModuleView { get { return mainPart.PagesContainer.MainPage.DemoModuleView; } set { mainPart.PagesContainer.MainPage.DemoModuleView = value; } }
		public DemoModuleControlOptionsView DemoModuleOptionsView { get { return mainPart.PagesContainer.MainPage.OptionsView; } set { mainPart.PagesContainer.MainPage.OptionsView = value; } }
		public bool IsOptionsExpanded { get { return mainPart.PagesContainer.MainPage.IsOptionsExpanded; } set { mainPart.PagesContainer.MainPage.IsOptionsExpanded = value; } }
		public bool IsLoading { get { return mainPart.PagesContainer.MainPage.ModuleWrapper.LoadingInProgress; } }
		public FlowDirection DemoFlowDirection { get { return mainPart.PagesContainer.MainPage.ModuleWrapper.DemoFlowDirection; } set { mainPart.PagesContainer.MainPage.ModuleWrapper.DemoFlowDirection = value; } }
		public FlowDirection WrapperViewFlowDirection { get { return mainPart.PagesContainer.MainPage.ModuleWrapper.View.FlowDirection; } }
		public event DepPropertyChangedEventHandler StateChanged;
		public event EventHandler<ModuleAppearEventArgs> ModuleAppear;
		
		public event EventHandler BackButtonClick;
		public event EventHandler BuyNowButtonClick;
		public event EventHandler ModuleReload;
		public event DepPropertyChangedEventHandler ActualPageChanged;
		public event DepPropertyChangedEventHandler AllowRunAnotherDemoChanged;
		public void ReloadModule() {
			if(ModuleReload != null)
				ModuleReload(this, EventArgs.Empty);
		}
		protected override void RegisterViews() {
			RegisterView(typeof(BaseControlMainPart), typeof(BaseControlMainPartView));
			RegisterView(typeof(BaseControlPagesContainer), typeof(BaseControlPagesContainerView));
			//RegisterView(typeof(DemoBaseControlProductsPage), typeof(BaseControlProductsPageView));
			RegisterView(typeof(BaseControlModulesPage), typeof(BaseControlModulesPageView));
			RegisterView(typeof(BaseControlMainPage), typeof(BaseControlMainPageView));
			//RegisterView(typeof(BaseControlModuleSelector), typeof(BaseControlModuleSelectorView));
			RegisterView(typeof(BaseControlToobar), typeof(BaseControlToobarView));
			RegisterView(typeof(BaseControlModuleWrapper), typeof(BaseControlModuleWrapperView));
		}
		protected override PartsControlPart CreateMainPart() {
			mainPart = new BaseControlMainPart(this);
			mainPart.ModuleAppear += OnMainPartModuleAppear;
			mainPart.BackButtonClick += OnMainPartBackButtonClick;
			mainPart.BuyNowButtonClick += OnMainPartBuyNowButtonClick;
			mainPart.PagesContainer.ActualPageChanged += OnPagesContainerActualPageChanged;
			
			return mainPart;
		}
		void RaiseAllowRunAnotherDemoChanged(DependencyPropertyChangedEventArgs e) {
			if(AllowRunAnotherDemoChanged != null)
				AllowRunAnotherDemoChanged(this, new DepPropertyChangedEventArgs(e));
		}
		void RaiseActualPageChanged(DependencyPropertyChangedEventArgs e) {
			if(ActualPageChanged != null)
				ActualPageChanged(this, new DepPropertyChangedEventArgs(e));
		}
		void OnPagesContainerActualPageChanged(object sender, ThePropertyChangedEventArgs<BaseControlPage> e) {
			ActualPage = e.NewValue;
		}
		
		void OnMainPartBackButtonClick(object sender, EventArgs e) {
			if(BackButtonClick != null)
				BackButtonClick(this, EventArgs.Empty);
		}
		void OnMainPartBuyNowButtonClick(object sender, EventArgs e) {
			if(BuyNowButtonClick != null)
				BuyNowButtonClick(this, EventArgs.Empty);
		}
		void RaiseStateChanged(DependencyPropertyChangedEventArgs e) {
			if(StateChanged != null)
				StateChanged(this, new DepPropertyChangedEventArgs(e));
		}
		void OnMainPartModuleAppear(object sender, ModuleAppearEventArgs e) {
			CurrentDemoModule = e.DemoModule;
			DemoModuleException = e.DemoModuleException;
			if(ModuleAppear != null)
				ModuleAppear(this, e);
		}
	}
}
